package com.xingzhi.crm.service;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.zxcms.user.bean.*;
import com.zxcms.user.mapper.CmsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by daniel.luo on 2018/6/12.
 */
@Service
public class CmsService {
    @Autowired
    CmsMapper cmsMapper;

    @Autowired
    UserService userService;

    public CourseLen doGetTotalLen(Map<String, Object> mapParam) throws Exception {
        return cmsMapper.getTotalLen(mapParam);
    }

    public Object delUseless(Map<String, Object> mapParam) throws Exception {
        return cmsMapper.delUseless(mapParam);
    }

    public CourseLen getTotalLenByCid(String cid) throws Exception {
        String catalog = userService.getCatalog(cid);
        CourseLen totalLen = new CourseLen();
        if (!StringUtils.isEmpty(catalog)) {
            JSONArray ja = JSONArray.parseArray(catalog);
            List<String> listId = getIds(ja, new ArrayList<String>());
            System.out.println("total nodesId : " + listId.size());
            listId.forEach(s -> System.out.println(s));

            Map<String, Object> mapParam = new HashMap<String, Object>();
            mapParam.put("listId", listId);
            totalLen = doGetTotalLen(mapParam);
            System.out.println("totalLen : " + totalLen.getTotal_len());
        }
        return totalLen;
    }

    public List<String> getIds(JSONArray ja, List<String> listId) throws Exception {
        JSONArray child = new JSONArray();
        for (Object one : ja) {
            listId.add(((JSONObject) one).getString("id"));
            JSONArray cja = ((JSONObject) one).getJSONArray("children");
            if (null == cja) continue;
            else if (cja.size() > 0) {
                child.addAll(((JSONObject) one).getJSONArray("children"));
            }
        }
        if (child.size() > 0) return getIds(child, listId);
        else return listId;
    }

    public int updateCourseTotalLen(Map<String, String> mapParam) throws Exception {
        return cmsMapper.updateTotalLen(mapParam);
    }

    public int updateCourseShotStatus(Map<String, String> mapParam) throws Exception {
        return cmsMapper.updateCourseShotStatus(mapParam);
    }

    public List<Personage> likePersonage(Map<String, String> mapParam) throws Exception {
        return cmsMapper.likePersonage(mapParam);
    }

    public boolean checkModifyManagerInfo(String name, String phone_num) throws Exception {
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(phone_num)) return false;
        else return true;
    }

    public User CheckNumberPhoneExist(String phone_num) throws Exception {
        List<User> listManager = cmsMapper.getPhoneNumManager(phone_num);
        if (listManager.size() > 0) return listManager.get(0);
        List<User> listStudent = cmsMapper.getPhoneNumStudent(phone_num);
        if (listStudent.size() > 0) return listStudent.get(0);
        return null;
    }

    public int modifyManager(Map<String, String> mapParam) throws Exception {
        return cmsMapper.modifyManager(mapParam);
    }

    public int updateManagerPassword(Map<String, String> mapParam) throws Exception {
        return cmsMapper.updateManagerPassword(mapParam);
    }

    public boolean existStudy(String cid) throws Exception {
        int count = cmsMapper.getStudyCount(cid);
        if (count > 0) return true;
        else return false;
    }

    public int removeCourse(String cid) throws Exception {
        return cmsMapper.removeCourse(cid);
    }

    public Manager getManagerOneByUid(String uid) throws Exception {
        return cmsMapper.getManagerOneByUid(uid);
    }

    public String getManagerPwdByuid(String uid) throws Exception {
        return cmsMapper.getManagerPwdByuid(uid);
    }

    public List<Record> getAdminActionRecord(Map<String, String> mapParam, int pageNum, int pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        return cmsMapper.getAdminActionRecord(mapParam);
    }


    public List<Study> getStudy(int pageNum,int pageSize, String uid) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        return cmsMapper.getStudy(uid);
    }

    public int delectStudyOne(Map<String, String> mapParam) throws Exception {
        return cmsMapper.delectStudyOne(mapParam);
    }

    public StudentOne getStudentOneByUid(String uid) throws Exception {
        return cmsMapper.getStudentOneByUid(uid);
    }

    public int updateCourseVideoEncryptStatusOk(String key) throws Exception {
        return cmsMapper.updateCourseVideoEncryptStatusOk(key);
    }

    public int updateCourseVideoEncryptStatusFail(Map<String, String> mapParam) throws Exception {
        return cmsMapper.updateCourseVideoEncryptStatusFail(mapParam);
    }

    public List<CourseEncrypt> getCourseByEncryptkey(String key) throws Exception {
        return cmsMapper.getCourseByEncryptkey(key);
    }

    public int setEncryptInfo(Map<String, String> mapParam) throws Exception {
        return cmsMapper.setEncryptInfo(mapParam);
    }

    public List<CourseEncrypt> getCourseByEncryptkeyByUrl(String url) throws Exception {
        return cmsMapper.getCourseByEncryptkeyByUrl(url);
    }

    public CourseWareModel getCourseWareById(String ware_id) throws Exception {
        return cmsMapper.getCourseWareById(ware_id);
    }

    public int updateEncryptTime(Map<String, String> mapParam) throws Exception {
        return cmsMapper.updateEncryptTime(mapParam);
    }

    public List<CourseWareModel> getAllCourseWareOnEncrypt() throws Exception {
        return cmsMapper.getAllCourseWareOnEncrypt();
    }

    public List<CourseGet> getCourseShotLikeName(int pageNum,int pageSize, Map<String, String> mapParam) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        return cmsMapper.getCourseShotLikeName(mapParam);
    }

    public int delCourseWare(String cid) throws Exception{
        return cmsMapper.delCourseWare(cid);
    }

    public int delCourseWareRelation(String cid) throws Exception{
        return cmsMapper.delCourseWareRelation(cid);
    }

    public CourseWareRelation getCourseWareRelation(String cid) throws Exception {
        return cmsMapper.getCourseWareRelation(cid);
    }

    public CourseWareModel getCourseWareByWareId(String ware_id) throws Exception {
        return cmsMapper.getCourseWareByWareId(ware_id);
    }

    public int addCoursewWare(Map<String, String> mapParam) throws Exception {
        return cmsMapper.addCoursewWare(mapParam);
    }

    public int updateCourseShot(Map<String, String> mapParam) throws Exception{
        return cmsMapper.updateCourseShot(mapParam);
    }

    public CourseShotImport getTnumTlen(String cid) throws Exception{
        return cmsMapper.getTnumTlen(cid);
    }
}
